document.addEventListener('DOMContentLoaded', function() {
    document.getElementById('registerForm').addEventListener('submit', function(e) {
        e.preventDefault();
        
        const username = document.getElementById('username').value.trim();
        const password = document.getElementById('password').value.trim();
        const confirmPassword = document.getElementById('confirmPassword').value.trim();
        const email = document.getElementById('email').value.trim();
        const phone = document.getElementById('phone').value.trim();
        let isValid = true;
        
        // 验证用户名
        if (username === '') {
            document.getElementById('usernameError').textContent = '请输入用户名';
            isValid = false;
        } else if (username.length < 4) {
            document.getElementById('usernameError').textContent = '用户名至少4个字符';
            isValid = false;
        } else {
            document.getElementById('usernameError').textContent = '';
        }
        
        // 验证密码
        if (password === '') {
            document.getElementById('passwordError').textContent = '请输入密码';
            isValid = false;
        } else if (password.length < 6) {
            document.getElementById('passwordError').textContent = '密码至少6个字符';
            isValid = false;
        } else {
            document.getElementById('passwordError').textContent = '';
        }
        
        // 验证确认密码
        if (confirmPassword === '') {
            document.getElementById('confirmPasswordError').textContent = '请确认密码';
            isValid = false;
        } else if (password !== confirmPassword) {
            document.getElementById('confirmPasswordError').textContent = '两次输入的密码不一致';
            isValid = false;
        } else {
            document.getElementById('confirmPasswordError').textContent = '';
        }
        
        // 验证邮箱
        if (email && !validateEmail(email)) {
            document.getElementById('emailError').textContent = '请输入有效的电子邮箱';
            isValid = false;
        } else {
            document.getElementById('emailError').textContent = '';
        }
        
        // 验证手机号
        if (phone && !validatePhone(phone)) {
            document.getElementById('phoneError').textContent = '请输入有效的手机号码';
            isValid = false;
        } else {
            document.getElementById('phoneError').textContent = '';
        }
        
        if (isValid) {
            // 检查用户名是否已存在
            const users = JSON.parse(localStorage.getItem('users')) || [];
            const userExists = users.some(u => u.username === username);
            
            if (userExists) {
                document.getElementById('usernameError').textContent = '该用户名已被注册';
                return;
            }
            
            // 创建新用户
            const newUser = {
                id: Date.now(),
                username,
                password,
                email: email || null,
                phone: phone || null,
                registerDate: new Date().toLocaleString(),
                profile: {
                    nickname: username,
                    gender: 'secret'
                }
            };
            
            users.push(newUser);
            localStorage.setItem('users', JSON.stringify(users));
            
            // 自动登录
            localStorage.setItem('currentUser', JSON.stringify(newUser));
            
            alert('注册成功！您已自动登录');
            window.location.href = 'index.html';
        }
    });
});